package com.kingsoft.dc.khaos.extender.util;

import com.alibaba.fastjson.JSONObject;
import com.kingsoft.dc.khaos.extender.meta.api.AuthRequest;
import com.kingsoft.dc.khaos.extender.meta.utils.HttpUtils;
import com.kingsoft.dc.khaos.extender.model.ParamConfig;
import com.ksyun.auth.client.AuthenticationHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author Chris J
 * @Date: 2021/09/01
 */
public class MaiaUtils {
    private static Logger logger = LoggerFactory.getLogger(MaiaUtils.class);

    public static void doPersistSql2Maia(String sql, ParamConfig paramConfig, String url, String errorLog) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("sql_str", sql);
        jsonObject.put("sql_type", paramConfig.getDsType());
        jsonObject.put("error_log", errorLog);
        Map header = getHeader(paramConfig);
        HttpUtils.doPost(url, header, jsonObject.toJSONString());
    }

    public static Map getHeader(ParamConfig paramConfig) {
        AuthRequest authRequest = new AuthRequest();
        String ak = authRequest.getAk();
        String sk = authRequest.getSk();
        AuthenticationHelper.getHeaderInterceptor(ak, sk, paramConfig.getAuthUrl());
        String token = AuthenticationHelper.getToken();
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Content-Type", "application/json");
        headerMap.put("X-AUTH-APP", ak);
        headerMap.put("X-AUTH-TOKEN", token);
        headerMap.put("SOURCE-AK", ak);
        return headerMap;
    }
}
